<?php
/**
 * Description: 交易记录模型
 * User: WuHao
 * Date: 2017/5/26
 * Time: 9:55
 */
namespace app\backend\model;
use think\Model;

class TradeRecord extends Model
{
    private $_memberJournal = 'z_member_journal';
    private $_lease         = 'z_contract_order';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取交易记录列表
     * @date   2017-05-26
     * @author wuhao
     *
     * @param  int             $mid       当前用户id
     * @param  string          $beginTime 开始时间
     * @param  string          $endTime   截止时间
     * @param  int             $feeType   费用类型  0=全部 1房租，2物业，3水，4电,  5公摊水，6公摊电, 7税费, 8其他费用
     * @param  float|string    $moneyFrom 金额上限(元)
     * @param  float|string    $moneyTo   金额下限(元)
     * @param  int             $payType   支付类型 0=全部 2通联支付宝，3通联微信, 4=提现操作， 5富友
     * @param  int             $buildingId  楼宇id
     * @param  array|bool      $fromId    对方账号
     * @param  array|bool      $orderNum  订单号
     * @param  int             $page      页数
     * @param  int             $pageSize  每页个数
     * @param  bool            $excel     是否导出
     *
     * @return array|bool
     */
    public function getTradeRecord($mid, $beginTime, $endTime, $feeType = 0, $moneyFrom = '', $moneyTo = '', $payType = 0,
                                   $buildingId, $fromId = false, $orderNum = false, $page = 1, $pageSize = 15, $excel = false)
    {
        if (!$mid || !$beginTime || !$endTime) {
            return false;
        }

        $where['m.mid'] = $mid;
        if ($buildingId) {
            $where['l.building_id'] = $buildingId;
        }
        if ($fromId !== false) {//使用对方名称条件
            if ($fromId == []) {
                return [];
            } else {
                $where['m.from_id'] = ['in', $fromId];
            }
        }

        if ($orderNum !== false) {//使用房号所搜返回订单号数组条件
            if ($orderNum == []) {
                return [];
            } else {
                $where['m.ordernum'] = ['in', $orderNum];
            }
        }

        if ($feeType != 0) {
            $where['m.mtype'] = $feeType;
        }
        if ($payType != 0) {
            if ($payType == 4) {
                $where['m.type'] = ['in', [4,7]];
            } else {
                $where['m.type'] = $payType;
            }
        }
        if (strtotime($beginTime) && strtotime($endTime)) {
            $begin = strtotime($beginTime);
            $end   = strtotime($endTime);
            $where['m.rectime'] = ['between', [$begin, $end]];
        }
        if ($moneyFrom != '' && $moneyTo != '' && is_numeric($moneyFrom) && is_numeric($moneyTo) && $moneyFrom <= $moneyTo) {
            $where['m.dmoney'] = ['between', [fen($moneyFrom), fen($moneyTo)]];
        }
        $field = 'm.mid, m.dmoney, m.action, m.ordernum, m.from_id, m.rectime, m.type, m.trade_no, m.mtype, m.begin_cycle, m.end_cycle';

        if ($excel === true) {
            $res = $this->setTable($this->_memberJournal .' m')->field($field)
                ->join($this->_lease . ' l', 'm.ordernum=l.ordernum', 'LEFT')->where($where)->select();
        } else {
            $res = $this->setTable($this->_memberJournal .' m')->field($field)
                ->join($this->_lease . ' l', 'm.ordernum=l.ordernum', 'LEFT')->where($where)->page($page, $pageSize)->select();
        }
        return $res ?: [];
    }

    /**
     * 获取交易记录列表数量
     * @date   2017-05-26
     * @author wuhao
     *
     * @param  int             $mid       当前用户id
     * @param  string          $beginTime 开始时间
     * @param  string          $endTime   截止时间
     * @param  int             $feeType   费用类型  0=全部 1房租，2物业，3水，4电,  5公摊水，6公摊电, 7税费, 8其他费用
     * @param  float|string    $moneyFrom 金额上限(元)
     * @param  float|string    $moneyTo   金额下限(元)
     * @param  int             $payType   支付类型 0=全部 2通联支付宝，3通联微信, 4=提现操作， 5富友
     * @param  int             $buildingId  楼宇id
     * @param  array|bool      $fromId    对方账号
     * @param  array|bool      $orderNum  订单号
     *
     * @return array|bool
     */
    public function cntTradeRecord($mid, $beginTime, $endTime, $feeType = 0, $moneyFrom = '', $moneyTo = '', $payType = 0,
                                   $buildingId, $fromId = false, $orderNum = false)
    {
        if (!$mid || !$beginTime || !$endTime) {
            return false;
        }

        $where['m.mid'] = $mid;
        if ($buildingId) {
            $where['l.building_id'] = $buildingId;
        }
        if ($fromId !== false) {//使用对方名称条件
            if ($fromId == []) {
                return [];
            } else {
                $where['m.from_id'] = ['in', $fromId];
            }
        }

        if ($orderNum !== false) {//使用房号所搜返回订单号数组条件
            if ($orderNum == []) {
                return [];
            } else {
                $where['m.ordernum'] = ['in', $orderNum];
            }
        }

        if ($feeType != 0) {
            $where['m.mtype'] = $feeType;
        }
        if ($payType != 0) {
            if ($payType == 4) {
                $where['m.type'] = ['in', [4,7]];
            } else {
                $where['m.type'] = $payType;
            }
        }
        if (strtotime($beginTime) && strtotime($endTime)) {
            $begin = strtotime($beginTime);
            $end   = strtotime($endTime);
            $where['m.rectime'] = ['between', [$begin, $end]];
        }
        if ($moneyFrom != '' && $moneyTo != '' && is_numeric($moneyFrom) && is_numeric($moneyTo) && $moneyFrom <= $moneyTo) {
            $where['m.dmoney'] = ['between', [fen($moneyFrom), fen($moneyTo)]];
        }

        $res = $res = $this->setTable($this->_memberJournal .' m')->join($this->_lease . ' l', 'm.ordernum=l.ordernum', 'LEFT')->where($where)->count();
        return $res ?: [];
    }
}